var API = require('API').survey.order;

module.exports = ['$scope','$routeParams',
    function ($scope,$routeParams) {
        $scope.data={};
        var id=$routeParams.id;
        API.get(id,function(json){
            $scope.$apply(function(){
                $scope.data=json;
            })
        });

        $scope.submit=function(){
            var order={
                id:$scope.data.id,
                user_name:$scope.data.user_name,
                user_id:$scope.data.user_id,
                customer_name:$scope.data.customer_name,
                customer_phone:$scope.data.customer_phone,
                name:$scope.data.name,
                create_time:$scope.data.create_time

            };
            var view_details=$scope.data.details;
            var details=[];
            $.each(view_details,function(i,v){
                var detail={
                    id: v.id,
                    sn: v.sn,
                    main_id: v.main_id,
                    question_id: v.question_id,
                    current_answer_id: v.current_answer_id,
                };
                details.push(detail);
            });
            //写入数据
            var i=0;
            var total=details.length;
            API.upd_order(order.id,order,function(){
                $.each(details,function(i,v){
                    API.upd_order_detail(order.id,v,function(){
                        i++;
                        if(i==total) alert('提交成功');
                    })
                })
            })
        }
    }];